// 订单功能实现
#include <windows.h>
#include <string.h>
#include <time.h>
#include "DishOrderManage.h"

extern char loginUserName[10]; // 登录名

// 定义了添加菜品函数
void DishOrderAdd(int orderCount, struct DishOrder *dishOrder, ListPointer detailOrderPointer, ListPointer dishOrderPointer, ListPointer menuPointer, char orderSavePath[])
{
    int count;
    int dishAmount;     // 菜份数
    char dishNumber[8]; // 菜编号
    FILE *filePointer;
    ListPointer listPointer;
    ListPointer tempListPointer;
    struct Menu *menuData;
    struct DetailOrder *detailMenuData;
    while (1)
    {
        CleanRow(orderCount + 3, orderCount + 12);
        SetCursorPosition(orderCount + 11, 0);
        printf("\t\t#@# 按[ Esc ]键退出...");
        SetCursorPosition(orderCount + 3, 0);
        memset(dishNumber, 0, sizeof(char));
        printf("\t\t请输入要增加的菜编号：");
        FormatInputString(dishNumber, '0', '9', 5, 2);
        if (strcmp(dishNumber, "0") == 0)
        {
            return;
        }
        printf("\n\t\t请输入要增加的份数：");
        dishAmount = FormatInputString(NULL, '1', '9', 1, 2);
        count = 0;
        listPointer = menuPointer->listNext;
        while (listPointer != NULL)
        {
            count++; //   计数第几道菜
            menuData = (struct Menu *)(listPointer->listData);
            // 找到菜单中对应的菜信息
            if (strcmp(menuData->dishNo, dishNumber) == 0)
            {
                break;
            }
            listPointer = listPointer->listNext;
        }
        if (listPointer == NULL)
        {
            printf("\n\t\t菜编号输入有误，请重新输入，按任意键继续...");
            getch();
        }
        else
        {
            break;
        }
    }
    printf("\n\n\t\t是否确认加菜(y)确定、(n)返回:");
    if (FormatInputString(NULL, 0, 0, 1, 1) == 'n')
    {
        printf("\n\t\t加菜失败，按任意键返回...");
        getch();
        return;
    }
    tempListPointer = detailOrderPointer->listNext;
    while (tempListPointer != NULL)
    {
        detailMenuData = (struct DetailOrder *)(tempListPointer->listData);
        // 判断详单中是否重复点菜
        if (strcmp(detailMenuData->detailMenu.dishNo, dishNumber) == 0)
        {
            // 份数增加
            detailMenuData->detailOrderAmount = dishAmount + (detailMenuData->detailOrderAmount);
            SetCursorPosition(count, 0);
            printf("\t\t*%-11s%-12s￥%-10d%-12d",
                   detailMenuData->detailMenu.dishNo, detailMenuData->detailMenu.dishName,
                   detailMenuData->detailMenu.dishPrice, detailMenuData->detailOrderAmount);
            break;
        }
        tempListPointer = tempListPointer->listNext;
    }
    if (tempListPointer == NULL)
    {
        // 没点过，新点菜添加到详单链表
        LinkedListAdd(detailOrderPointer, DetailOrderInit(dishOrder, menuData, dishAmount));
        SetCursorPosition(count, 0);
        printf("\t\t*%-11s%-12s￥%-10d%-12d", menuData->dishNo, menuData->dishName,
               menuData->dishPrice, dishAmount);
    }
    // 详单信息写入文件
    filePointer = FileOpen(orderSavePath);
    FileWrite(filePointer, detailOrderPointer, sizeof(struct DetailOrder), 0);
    DishOrderStatist(dishOrder, detailOrderPointer);
    // 订单更新信息写入到文件
    filePointer = FileOpen("tempDishOrders.txt");
    FileWrite(filePointer, dishOrderPointer, sizeof(struct DishOrder), 0);
    SetCursorPosition(orderCount + 1, 0);
    printf("\t\t\t\t\t%-4s:%-5d%-4s:%d\n", "总金额", dishOrder->orderTotalMoney,
           "总份数", dishOrder->orderAmount);
    SetCursorPosition(orderCount + 7, 0);
    printf("\t\t加菜成功");
    printf("\n\t\t是否继续加菜，(y)确定、(n)返回：");
    if (FormatInputString(NULL, 0, 0, 1, 1) == 'y')
    {
        DishOrderAdd(orderCount, dishOrder, detailOrderPointer,
                     dishOrderPointer, menuPointer, orderSavePath);
    }
}
// 减菜
void DishOrderDelete(int orderCount, struct DishOrder *dishOrder,
                     ListPointer detailOrderPointer, ListPointer menuPointer, ListPointer dishOrderPointer, char orderSavePath[])
{
    int dishAmount;     // 菜份数
    char dishNumber[8]; // 菜编号
    int count;          // 记录输入菜的位置
    FILE *filePointer;
    ListPointer listPointer;
    ListPointer tempListPointer;
    struct DetailOrder *detaiOrderData;
    CleanRow(orderCount + 3, orderCount + 12);
    SetCursorPosition(orderCount + 11, 0);
    printf("\t\t#@# 按[ Esc ]键退出...");
    SetCursorPosition(orderCount + 3, 0);
    memset(dishNumber, 0, sizeof(char));
    printf("\t\t请输入退掉的菜编号：");
    FormatInputString(dishNumber, '0', '9', 5, 2);
    if (strcmp(dishNumber, "0") == 0)
    {
        return;
    }
    printf("\n\t\t请输入要退掉的份数：");
    dishAmount = FormatInputString(NULL, '1', '9', 1, 2);
    count = 0;
    listPointer = menuPointer->listNext;
    while (listPointer != NULL)
    {
        count++; // 计数第几道菜
        // 找到菜谱中对应的菜信息
        if (strcmp(((struct Menu *)(listPointer->listData))->dishNo, dishNumber) == 0)
        {
            break;
        }
        listPointer = listPointer->listNext;
    }
    tempListPointer = detailOrderPointer;
    listPointer = detailOrderPointer->listNext;
    while (listPointer != NULL)
    {
        detaiOrderData = (struct DetailOrder *)(listPointer->listData);
        // 判断详单中是否有此道菜
        if (strcmp(detaiOrderData->detailMenu.dishNo, dishNumber) == 0)
        {
            // 要退的份数大于实际点过的份数
            if (dishAmount > detaiOrderData->detailOrderAmount)
            {
                printf("\n\t\t输入份数超过实际份数，按任意键继续...");
                getch();
                break;
            }
            else
            {
                printf("\n\n\t\t是否确认退菜(y)确定、(n)返回:");
                if (FormatInputString(NULL, 0, 0, 1, 1) == 'n')
                {
                    printf("\n\t\t退菜失败，按任意键继续...");
                    getch();
                    return;
                }
                // 更新份数
                detaiOrderData->detailOrderAmount = (detaiOrderData->detailOrderAmount) -
                                                    dishAmount;
                // 退的份数是否等于点过的份数
                if (detaiOrderData->detailOrderAmount == 0)
                {
                    SetCursorPosition(count, 0);
                    printf("\t\t%-12s%-12s￥%-10d%-12d",
                           detaiOrderData->detailMenu.dishNo,
                           detaiOrderData->detailMenu.dishName,
                           detaiOrderData->detailMenu.dishPrice,
                           detaiOrderData->detailOrderAmount);
                    tempListPointer->listNext = listPointer->listNext;
                    free(listPointer);
                    filePointer = fopen(orderSavePath, "w+");
                }
                else
                {
                    filePointer = FileOpen(orderSavePath);
                    SetCursorPosition(count, 0);
                    printf("\t\t*%-11s%-12s￥%-10d%-12d", detaiOrderData->detailMenu.dishNo,
                           detaiOrderData->detailMenu.dishName, detaiOrderData->detailMenu.dishPrice,
                           detaiOrderData->detailOrderAmount);
                }
                // 更新文件数据库
                FileWrite(filePointer, detailOrderPointer, sizeof(struct DetailOrder), 0);
                DishOrderStatist(dishOrder, detailOrderPointer);
                filePointer = FileOpen("tempDishOrders.txt");
                FileWrite(filePointer, dishOrderPointer, sizeof(struct DishOrder), 0);
                SetCursorPosition(orderCount + 1, 0);
                printf("\t\t\t\t\t%-4s:%-5d%-4s:%d\n", "总金额", dishOrder->orderTotalMoney,
                       "总份数", dishOrder->orderAmount);
                SetCursorPosition(orderCount + 6, 0);
                printf("\n\t\t退菜成功，按任意键继续...");
                getch();
                return;
            }
        }
        tempListPointer = listPointer;
        listPointer = listPointer->listNext;
    }
    if (listPointer == NULL)
    {
        printf("\n\t\t此道菜未点或不存在，无法退菜，按任意键继续...");
        getch();
    }
}
